<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\JubaoModel;
use cmf\controller\HomeBaseController;
use think\Request;

class JubaoController extends HomeBaseController
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->assign("title", "举报");
        $this->assign("keyword", "举报");
        $this->assign("description", "举报");
        $this->assign("ip", $this->request->ip());
    }

    public function index() {

        return $this->fetch(':jubao');
    }
    // 提交举报
    public function dosubmit() {
        $captcha = $this->request->request('verify_code');
        if(!$captcha) {
            return $this->error('请输入验证码');
        }
        if(!captcha_check($captcha)){
            //验证失败
            return $this->error('验证码错误');
        };
        $reportType = (int)$this->request->request("report_type");
        if($reportType != 0 && $reportType != 1) {
            return $this->error('参数错误');
        }
        $username = $this->request->request('username');
        $phone = $this->request->request('phone');
        $idno = $this->request->request('idno');
        $reportedName = $this->request->request('reported_name');
        $reportedJob = $this->request->request('reported_job');
        $title = $this->request->request('title');
        $content = $this->request->request('content');
        $attachList = $this->request->request('attachlist');
        $ip = $this->request->ip();
        $attachList = $attachList ? htmlspecialchars_decode($attachList): "[]";
        if($reportType == 1) {
            if(!$username || !$phone || !$idno) {
                return $this->error('请输入您的相关身份信息');
            }
            if(strlen($idno) != 16 && strlen($idno) != 18) {
                return $this->error('身份证号码错误');
            }
        }
        if(!$reportedName || !$reportedJob) {
            return $this->error('请输入被举报人信息');
        }

        if(!$title) {
            return $this->error('请输入举报标题');
        }
        if(!$content) {
            return $this->error("请输入举报内容");
        }
        // 生成举报码
        $code = uniqid();
        $data = [
            'report_code' => $code,
            'reported_name' => $reportedName,
            'report_type' => $reportType,
            'reported_job' => $reportedJob,
            'title' => $title,
            'content' => $content,
            'attachlist' => $attachList,
            'ip' => $ip
        ];
        if($reportType == 1) {
            $data['username'] = $username;
            $data['phone'] = $phone;
            $data['idno'] = $idno;
        }
        $jubaoModel = new JubaoModel();
        $jubaoModel->insert($data);
        $this->success("举报提交成功", null, ['code' => $code]);
    }

    public function jubao_success() {
        $code = $this->request->get('code');
        if(!$code) {
            return '';
        }
        $this->assign('code', htmlspecialchars($code));
        return $this->fetch(":jubao_success");
    }

    /**
     * 举报查询页面
     */
    public function query() {
        $this->assign("title", "举报查询");

        return $this->fetch(":jubao_query");

    }

    /**
     * 举报查询接口
     */
    public function doquery() {
        $code = $this->request->get('code');
        if(!$code) {
            return $this->error('请输入查询码');
        }
        $captcha = $this->request->get('verify_code');
        if(!$captcha) {
            return $this->error('请输入验证码');
        }
        if(!captcha_check($captcha)){
            //验证失败
            return $this->error('验证码错误');
        };
        $jubaoModel = new JubaoModel();
        $result = $jubaoModel->where(['report_code' => $code])->find();
        if(!$result) {
            return $this->error('查询码不存在');
        }
        return $this->success('','', $result);
    }

    public function detail() {

        $id = (int)$this->request->route("id");
        if(!$id) {
            $this->error('非法访问');
        }
        $model = new JubaoModel();
        $data = $model->find($id);
        if(!$data) {
            return $this->error('举报不存在');
        }
        $this->assign("title", $data['title']);
        $this->assign('data', $data);
        return $this->fetch(":jubao_detail");
    }
}
